package com.asus.api;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import android.R.string;
import android.util.Log;

public class ApiBase
{
	protected HashMap<String, String> mHashMap;
	protected static HttpClient mHttpClient = new DefaultHttpClient();
	protected HttpGet mHttpGet = new HttpGet();
	protected Element mRootElement;
	protected Boolean mIsQuerySuccess = false;
	protected static String mUsername;
	protected static String mUserpass;	
	protected static String mTicket = "";
	protected static String mCusId = "";	
	
	protected ApiBase(String _Path, HashMap<String, String> pHashMap)
	{
		StringBuilder _Builder = new StringBuilder(_Path);

		
		pHashMap.put("CallerID", "appCoEvO@9co!M");
		pHashMap.put("CallerPW", "J0m#25p$8@");
		pHashMap.put("SessionID", "aaa789");
		pHashMap.put("IMEI", "aaa");
		pHashMap.put("Ticket",mTicket );
		pHashMap.put("CusID", mCusId);
		mHashMap = pHashMap;

		Set<Entry<String, String>> _EntrySet = pHashMap.entrySet();

		for (Entry<String, String> entry : _EntrySet)
		{
			_Builder.append("&");
			_Builder.append(entry.getKey());
			_Builder.append("=");
			_Builder.append(entry.getValue());
		}
		
		mHttpGet = new HttpGet(_Builder.toString());
		Log.i("Macalog", _Builder.toString());
	}
	
	protected Boolean Run()
	{
		try
		{
			HttpResponse _HttpResponse = mHttpClient.execute(mHttpGet);
			InputStream _InputStream = _HttpResponse.getEntity().getContent();
			
			DocumentBuilderFactory _Factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder _Builder = _Factory.newDocumentBuilder();
			Document _Document = _Builder.parse(_InputStream);
			mRootElement = _Document.getDocumentElement();
			
			return true;
			
		}
		catch (Exception e) {
			Log.i("Macalog", e.toString());
			return false;
		}
	}

}
